import { createRouter, createWebHashHistory } from 'vue-router'
const routes = [
    { path: '/', redirect: '/home' },
    { path: '/login', name: 'login', component: () => import('../views/Login.vue') },
    {
        path: '/home', name: 'home', component: () => import('../views/Home.vue'), redirect: '/welcome', children: [
            { path: '/welcome', component: () => import('../views/Welcome.vue') },
            { path: '/users', name: 'user', component: () => import('../views/User.vue') },
            { path: '/roles', name: 'roles', component: () => import('../views/power/Roles.vue') },
            { path: '/rights', name: 'right', component: () => import('../views/power/Rights.vue') },
            { path: '/goods', name: 'goods', component: () => import('../views/goods/GoodList.vue') },
            { path: '/add', name: 'add', component: () => import('../views/goods/addgoods.js') },
            { path: '/params', name: 'params', component: () => import('../views/Cates/Params.vue') },
            { path: '/categories', name: 'categories', component: () => import('../views/Cates/Cates.vue') }
        ]
    }
]
const router = createRouter({
    history: createWebHashHistory(),
    routes
})
router.beforeEach((to, from, next) => {
    // 如果请求的是login就放行
    if (to.path == '/login') return next()
    // 如果请求的不是login那么我们需要获取本地token来判断是否登录 
    const localToken = window.sessionStorage.getItem('token')
    if (!localToken) return next('/login')
    next()
})

export default router